package mkis.service.root.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mkis.service.root.constants.Constants;
import mkis.service.root.data.model.Member;
import mkis.service.root.other.LoginChk;
import mkis.service.root.service.MemberService;
import mkis.service.root.utils.EncryptionUtils;
import mkis.service.root.utils.HttpUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class LoginCTR {
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Resource(name="memberService")
	private MemberService memberService;
	
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home(Locale locale, Model model) throws Exception {
		ModelAndView mav=new ModelAndView("forward:/login.do");
		return mav;
	}
	
	@RequestMapping(value = "/login.do", method = RequestMethod.GET)
	public ModelAndView login(Locale locale, Model model) throws Exception {
		ModelAndView mav=new ModelAndView("login/login");
		
		return mav;
	}
	// /bbs//login_check.do
	
	@RequestMapping(value = "/login_check.do", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView login_check(HttpServletRequest request,Member paramMember,Model model,HttpServletResponse response,String id_save) throws Exception {
		ModelAndView mav=new ModelAndView("login/loginResult");
		String mb_id=paramMember.getMb_id();
		String mb_password=paramMember.getMb_password();
		Map<String,Object> queryParam=new HashMap<String,Object>();
		
		queryParam.put("mb_id", mb_id);
		queryParam.put("mb_password", mb_password);
		
		Member member=memberService.getLoginMember(queryParam);
		String status=null;
		String message=null;
		if(member==null){
			status="fail";
			message="잘못된 계정입니다. 아이디 및 비밀번호를 다시 확인해주세요.";
		} else{
			message=LoginChk.getMessge(mb_id, mb_password, member);
		}
		
		
		
		if(message==null){
			
			
			//HttpUtils.setSessionValue(request, "ss_mb_id", member.getMb_id());
			HttpUtils.setSessionValue(request, Constants.SESSION_MEMBER, member);
			HttpUtils.setSessionValue(request, Constants.SESSION_MEMBER_KEY, EncryptionUtils.encryptMd5(System.currentTimeMillis()+request.getHeader("HOST")+request.getHeader("user-agent")));
			
			/*
			 * String auto_login=param.get("auto_login");
			 * if (auto_login!=null && !"".equals(auto_login)) {
			    // 자동로그인 ---------------------------
			    // 쿠키 한달간 저장
				String sessionKey=request.getHeader("HOST")+request.getRemoteAddr()+request.getHeader("user-agent")+member.getMb_password();
				String key=EncryptionUtils.encryptMd5(sessionKey);
				
				Cookie[] cookies=request.getCookies();
				
				HttpUtils.setCookie("ck_mb_id", member.getMb_id(), 86400 * 31, "/", response);
				HttpUtils.setCookie("ck_auto", key, 86400 * 31, "/", response);
				
			    // 자동로그인 end ---------------------------
			} else {
				HttpUtils.setCookie("ck_mb_id", "", 0, "/", response);
				HttpUtils.setCookie("ck_auto", "", 0, "/", response);
			}*/

			// 아이디 자동저장 
			
			/* 한성 코드 추가 2015.01.16*/
			if(id_save!=null && !"".equals(id_save)) { 
				HttpUtils.setCookie("ck_id_save", member.getMb_id(), -1, "/", response);
			}else{ 
				HttpUtils.setCookie("ck_id_save", "", 0, "/", response);
			} 
			
			status="success";
			
		}else {
			status="fail";
		}
		model.addAttribute("msg", message);
		model.addAttribute("mode", status);
		
		return mav;
	}
	
	
	@RequestMapping(value = "/logout.do", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView logout(HttpServletRequest request){
		ModelAndView mav=new ModelAndView("redirect:/");
		
		HttpUtils.invalidateSession(request);
		return mav;
	}
	@RequestMapping(value = Constants.NOSESSION_URL, method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView noSession(HttpServletRequest request){
		ModelAndView mav=new ModelAndView("noSession");
		
		return mav;
	}
	
}
